#include "utils.h"

int union_find(int *array, int index) {
  int s = array[index];
  if (array[s] == s) {
    return s;
  } else {
    std::lock_guard<std::mutex> lk(union_find_mtx);
    do {
      s = array[s];
    } while (array[s] != s);
    do {
      int tmp = array[index];
      array[index] = s;
      index = tmp;
    } while (index != s);
    return s;
  }
}